//Requires prototype, proto.menu, 

///*Old Style*/Event.observe(window, 'load', init, false);
document.observe('dom:loaded', initUBoat);

var activeLine=null;
var bLineNumbers=true;
var bSidebar = true;
var bInlineNotes = true;

function initUBoat() {
	initMenubar();
	initNoteLines();
	initMenuNeptuner();
	initSource();
}

function initMenubar(){
	new ProtoFish('menuUBoat', '200', 'hover', true, true, true);
}

function initNoteLines(){
	for (i = 1; i <= nSourceLines; i++){
	    var locationid='SL'+i;
	    InPlaceNote.makeEditable( {
		    type: 'textarea',
		    id: locationid,
		    save_url: 'edit.php'
	    } );
	    $(locationid).observe('click', onClickSourceLine)
	}
}

function initMenuNeptuner(){
	var myMenuItems = [
	  {
	    name: 'Goto Codebase Home',
	    className: 'edit',
	    callback: function() {
	      window.location = sNeptunerHome + "index.html";
	    }
	  },
	  {
	    name: 'Goto Submarine View',
	    className: 'delete',
	    callback: function() {
	    	window.location = sNeptunerHome + 'submarine/' + sFilename + '.usub.html';
	    }
	  },
	  {
	    separator: true
	  },
	  {
	    name: 'About',
	    className: 'save',
	    callback: function() {
	      alert('Neptuner UBoat \n (C) 2009 The Oraclique \n Developed by SonOfDelphi');
	    }
	  }
	];

	new Proto.Menu({
	  selector: '#menuNeptuner', // context menu will be shown when element with id of "contextArea" is clicked
	  className: 'menu desktop', // this is a class which will be attached to menu container (used for css styling)
	  menuItems: myMenuItems, // array of menu items
	  activator: 'click'
	});
}

function initSource(){
//	$$("#paneSource ol li").each(addClicker);
	
	var codeLineMenuItems = [
	  {
	    name: 'Add Bookmark',
	    className: 'edit',
	    callback: addBookmark
	  },
	  {
	    name: 'Show Notes',
	    className: 'delete',
	    callback: showNotes
	  },
	  {
	    name: 'Hide Notes',
	    className: 'delete',
	    callback: hideNotes
	  }
	];

	new Proto.Menu({
	  selector: 'li img', // context menu will be shown when element with id of "contextArea" is clicked
	  className: 'menu desktop', // this is a class which will be attached to menu container (used for css styling)
	  menuItems: codeLineMenuItems, // array of menu items
	  activator: 'click'
	});
	
	activeLine=$('SL1');
	activateLine();
}


function onClickSourceLine(ev){
	// Code to respond to each click goes here
	deactivateLine();
	activeLine = findTargetLine(ev);
	activateLine();
	//alert(activeLine.identify());
}

function deactivateLine()
{
	if(activeLine != null)
		activeLine.removeClassName('active');
}

function activateLine(element)
{
	if(activeLine !=null){
		activeLine.addClassName('active');
		$('barStatus').update("Line "+ activeLine.identify());
	}
}

function toggleLineNumbers()
{
	if(bLineNumbers == true)
	{
		$$("#paneSource ol").each(function(elmt) {
			elmt.addClassName('nolinenumbers')				
		});
		bLineNumbers=false;
	}
	else
	{
		$$("#paneSource ol").each(function(elmt) {
			elmt.removeClassName('nolinenumbers')				
		});
		bLineNumbers=true;
	}
}

function addBookmark(evt)
{
	var target = evt.target.next(0);
	var location = target.identify();
	//alert("Bookmark" + location);
	
	var addToLocation=$('listBookmarks');

	var bookmark = document.createElement('a');
	var item = document.createElement('li');
	
	var buttonClicker = document.createElement('img');
	buttonClicker.setAttribute('src', sUBoatHome + 'bookmark.png');
	buttonClicker.setAttribute('class', 'clicker'); // For IE :(

	bookmark.setAttribute('href', "#"+location);
	bookmark.insert("Line "+ location + "\n");
	item.insert({top:bookmark});
	item.insert({top:buttonClicker});
	addToLocation.insert({top:item});
	
	var bookmarkID = bookmark.identify();
	EditInPlace.makeEditable( {
		type: 'text',
		id: bookmarkID,
		save_url: 'edit.php',
		is_wrapped:true
	} );
}

function addScrap()
{
	var addToLocation=$('listScraps');
	var buttonClicker = document.createElement('img');
	buttonClicker.setAttribute('src',  sUBoatHome + 'note.png');
	buttonClicker.setAttribute('class', 'clicker'); // For IE :(
	
	var scrap = document.createElement('span');
	var item = document.createElement('li');
	var newLine1 = document.createElement('br');
	var newLine2 = document.createElement('br');
	
	var text= $('textareaScrap').value;
	scrap.insert(text);
	
	item.insert({top:scrap});
	item.insert({top:buttonClicker});
	item.insert({bottom:newLine1})
	item.insert({bottom:newLine2});
	addToLocation.insert({top:item});
	
	var scrapID = scrap.identify();
	EditInPlace.makeEditable( {
		type: 'textarea',
		id: scrapID,
		save_url: 'edit.php',
		is_wrapped:true
	} );
}

function clearScrap()
{
	$('textareaScrap').value="";
}

function removeNotes(evt)
{
	var target = evt.target.up(0);
	alert("RemoveNotes" + target.identify());
}

function saveFile()
{
	saveCurrentFile();
}

function addClicker(elmt)
{
	if(elmt.firstDescendant().tagname == 'img'){
		return;
	}
	
	var buttonClicker = document.createElement('img');
	buttonClicker.setAttribute('src',  sUBoatHome + 'code.png');
	buttonClicker.setAttribute('class', 'clicker'); // For IE :(
	
	elmt.insert({top:buttonClicker});
}

function toggleSidebar()
{
	//alert('sidebar');
	if(bSidebar==true){
		$('sideBarContents').hide();
		bSidebar=false;
	}
	else{
		$('sideBarContents').show();
		bSidebar=true;
	}
}

function toggleInlineNotes()
{
	//alert('inline');
	if(bInlineNotes==true){
		$$('.ilnote').each(function (elmt){elmt.hide()});
		bInlineNotes=false;
	}
	else{
		$$('.ilnote').each(function (elmt){elmt.show()});
		bInlineNotes=true;
	}
}

function findTargetLine(evt)
{
	var looper = evt.target;
	
	while(!looper.hasClassName('sl')){
		looper = looper.up(0);
	}
	
	return looper;
}

//Not used, didn't work
function addNote(evt)
{
	var target = evt.target.next(0);
	var newEvent = target['dblclick'];
	if (typeof(newEvent) == "function") {
		target[eventName]();
	} else {}	

}

function addInlineNote()
{
	var text= $('textareaScrap').value;
	attachNote(activeLine, text);
}

function hideNotes(evt)
{
	var target = evt.target.next(0);
	target.siblings().each(function (elmt) {if(elmt.hasClassName('ilnote')) elmt.hide();})
}

function showNotes(evt)
{
	var target = evt.target.next(0);
	target.siblings().each(function (elmt) {if(elmt.hasClassName('ilnote')) elmt.show();})
}

function gotoLine()
{
	var gotoLocation = $('SL' + $('inpLine').value);
	if(gotoLocation){
		$(gotoLocation).scrollTo();
		$('inpLine').value="";
		deactivateLine();
		activeLine = $(gotoLocation);
		activateLine();
	}
}